/*
 * logon_session.cpp
 *
 *  Created on: Aug 2, 2013
 *      Author: killerg
 */


#include "server_deps.h"
#include "server.h"

#include "server_cfg.h"
#include "logon_session.h"
#include "auth_mgr.h"
#include "server_console.h"

#include "share/connection_mgr.h"
#include "share/cfg_file.h"
#include "share/sl_msg.pb.h"
#include "share/clock.h"
#include "share/msg.h"


#include "share/msg_handler.h"

#define  g_lmsg_handler Singleton<msg_handler_t<sl::LMsg>>::Instance()


void handle_llogon(sl::LMsg* msg) {
    UTIL_LOG(T, "connect logon %s", msg->logon().ok() ? "ok" : "error");
}
void handle_lplayerwilllogon(sl::LMsg* msg) {
	if (g_svr.status != co::ESS_Running) {
		return;
	}

	auto lplayerwilllogon = msg->playerwilllogon();
    int64_t playerid = lplayerwilllogon.playerid();
    string account = lplayerwilllogon.account();
    int32_t serverid = playerid / co::MaxAccountNum;
    if (serverid != g_cfg->ServerId)
        return;

    //LOG_T("playerid %d will logon with key %d", playerlogon.playerid(), playerlogon.key());
    g_authmgr->auth_data[playerid] = {
        account,
        lplayerwilllogon.channel(),
        playerid,
        lplayerwilllogon.key(),
        lplayerwilllogon.mc(),
        20,
        lplayerwilllogon.active(),
    };

    CREATE_SL_SMSG(SPlayerWillLogon, playerwilllogon);
    playerwilllogon->set_sessid(lplayerwilllogon.sessid());
    g_logonsess->tran.send(___msg);
}

bool logon_session_t::init() {
    bool ok = base::init(
            fmt_cstr("%s:%d",
                    g_cfg->LogonIp.c_str(),
                    g_cfg->LogonPort),
            [this](){
                UTIL_LOG(I, "logon connected");

                CREATE_SL_SMSG(SLogon, logon);
                logon->set_serverid(g_cfg->ServerId);
                logon->set_ip(g_cfg->ListenClientIp);
                logon->set_port(g_cfg->ListenClientPort);
                base::tran.send(___msg);
            },
            [this](){
                UTIL_LOG(I, "logon disconnected");
            },
            false
            );
    if (!ok) {
        UTIL_LOG(E, "connect to logon at %s:%d failed",
                g_cfg->LogonIp.c_str(),
                g_cfg->LogonPort);
        return false;
    }
    UTIL_LOG(I, "connect to logon at %s:%d",
            g_cfg->LogonIp.c_str(),
            g_cfg->LogonPort);

    ok = ok && base::reg(sl::ID_LLogon      ,handle_llogon      );
    ok = ok && base::reg(sl::ID_LPlayerWillLogon,handle_lplayerwilllogon);

    return ok;
}

void logon_session_t::send_server_status() {
	CREATE_SL_SMSG(SStatus, status);
	status->set_online_num(g_sessmgr->base::connmgr.size());
	status->set_status(g_svr.status);
	base::tran.send(___msg);
}

void logon_session_t::update() {
    if (base::tran.is_connected()) {
        static double report_timer = 5.0f;
        if (report_timer >= 0) {
            report_timer -= g_clock->delta;
            if (report_timer < 0 || last_server_status != g_svr.status) {
                report_timer = 5.0f;

				send_server_status();
			}
        }
    }
    base::update();
    last_server_status = g_svr.status;
}
